"""
作者：元明
日期：2023年11月03日
"""

# 高性能，高内聚/低耦合，高可用，负载均衡


'''
练习：
1. 99乘法表输出
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
....
1x9=9 ..... 9x9=81
2. 对数字列表中数字的求和，求平均值，求最大值，求最小值
3. 输入任意数字求和，求平均值，求最大值，求最小值
4. 冒泡排序实现
'''


# 定义一个用来显示菜单的函数
def menu():
    print("*" * 20)
    print("1、实战练习一")
    print("2、实战练习二")
    print("3、实战练习三")
    print("4、实战练习四")
    print("5、退出")
    print("*" * 20)


# 实战练习一
def case_1():
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f"{j}*{i}={i * j}", end=" ")
        print()


# 实战练习二
def case_2():
    nums = [1, 88, 9, 666, 23, -4, 87]
    sum = 0
    avg = None
    max_nums = nums[0]
    min_nums = nums[1]
    for i in nums:
        sum += i
        avg = sum / len(nums)
        if i > max_nums:
            max_nums = i
        if i < min_nums:
            min_nums = i
    print("sum:", sum)
    print("avg:", avg)
    print("max_nums:", max_nums)
    print("min_nums:", min_nums)


# 实战练习三： 输入任意数字求和，求最大值、最小值、平均值。直到输入 bye 结束
def case_3():
    sum = 0
    num = 0
    avg = None
    max_nums = None
    min_nums = None
    flag = False
    while True:
        n = input("输入任意数字：")
        if n == 'bye':
            break
        else:
            # 遇到负数
            if n.startswith("-"):  # -123123
                n = n[1:]  # 123123
                flag = True
            if n.isdigit():
                n = int(n)
                # 遇到负数
                if flag == True:
                    n *= -1  # -123123
                num += 1
                sum += n
                avg = sum / num
                if max_nums == None:
                    max_nums = n
                elif max_nums < n:
                    max_nums = n
                if min_nums == None:
                    min_nums = n
                elif min_nums < n:
                    min_nums = n
            # 遇到 小数
            elif n.find(".") != -1:
                nums = n.split('.')
                if len(nums) == 2:
                    if nums[0].isdigit() and nums[1].isdigit():
                        n1 = int(nums[0])
                        n2 = float("0." + nums[1])
                        n = n1 + n2
                n = float(n)

    print("SUM:", sum)
    print("NUM:", num)
    print("AVG:", avg)
    print("MAX:", max_nums)
    print("MIN:", min_nums)


# 冒泡排序
def case_4():
    n = [1, 99, 888, 72, -8, 9999]
    for i in range(len(n) - 1):
        for j in range(len(n) - i - 1):
            if n[j] > n[j + 1]:
                n[j], n[j + 1] = n[j + 1], n[j]
    print(n)


# 定义一个启动函数
def start():
    while True:
        menu()
        op = input("请输入一个数字选择对应的功能：")
        if op == '1':
            case_1()
        elif op == '2':
            case_2()
        elif op == '3':
            case_3()
        elif op == '4':
            case_4()
        elif op == '5':
            break
        else:
            print("输入数字不在正确范围内,请重新选择输入！")


if __name__ == '__main__':
    start()
